package com.ekuaitu.kuaitu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.media.WantuService;
import com.alibaba.sdk.android.media.upload.UploadListener;
import com.alibaba.sdk.android.media.upload.UploadTask;
import com.alibaba.sdk.android.media.utils.FailReason;
import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.bean.ChangeInfoBean;
import com.ekuaitu.kuaitu.bean.CommitCertificateBean;
import com.ekuaitu.kuaitu.bean.LogoffBean;
import com.ekuaitu.kuaitu.bean.PersonalInfoBean;
import com.ekuaitu.kuaitu.bean.RechargeAdBean;
import com.ekuaitu.kuaitu.bean.UploadImageBean;
import com.ekuaitu.kuaitu.utils.AppManager;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.ImageLoader;
import com.ekuaitu.kuaitu.utils.KuaituDialog;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.SDCardUtils;
import com.ekuaitu.kuaitu.utils.SharedPreferencesKey;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.ekuaitu.kuaitu.utils.VerControlUtils;
import com.ekuaitu.kuaitu.widget.EasyTransition;
import com.michael.selectimage.PhotoPickerActivity;
import com.michael.selectimage.utils.BitmapUtils;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.zhy.autolayout.AutoRelativeLayout;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonalInfoActivity extends AppCompatActivity implements UploadListener {

    @BindView(R.id.personalinfo_return)
    ImageView personalinfoReturn;
    @BindView(R.id.personalinfo_toolbar)
    AutoRelativeLayout personalinfoToolbar;
    @BindView(R.id.personalinfo_arow)
    ImageView personalinfoArow;
    @BindView(R.id.personalinfo_headerRl)
    AutoRelativeLayout personalinfoHeaderRl;
    @BindView(R.id.personalinfo_tvNi)
    TextView personalinfoTvNi;
    @BindView(R.id.personalinfo_arow1)
    ImageView personalinfoArow1;
    @BindView(R.id.personalinfo_niRl)
    AutoRelativeLayout personalinfoNiRl;
    @BindView(R.id.personalinfo_tvSex)
    TextView personalinfoTvSex;
    @BindView(R.id.personalinfo_arow2)
    ImageView personalinfoArow2;
    @BindView(R.id.personalinfo_sexRl)
    AutoRelativeLayout personalinfoSexRl;
    @BindView(R.id.personalinfo_tvBath)
    TextView personalinfoTvBath;
    @BindView(R.id.personalinfo_arow3)
    ImageView personalinfoArow3;
    @BindView(R.id.personalinfo_bathRl)
    AutoRelativeLayout personalinfoBathRl;
    @BindView(R.id.personalinfo_tvPhone)
    TextView personalinfoTvPhone;
    @BindView(R.id.personalinfo_arow4)
    ImageView personalinfoArow4;
    @BindView(R.id.personalinfo_phoneRl)
    AutoRelativeLayout personalinfoPhoneRl;
    @BindView(R.id.personalinfo_freeTv)
    TextView personalinfoFreeTv;
    @BindView(R.id.personalinfo_tvFree)
    TextView personalinfoTvFree;
    @BindView(R.id.personalinfo_arow5)
    ImageView personalinfoArow5;
    @BindView(R.id.personalinfo_freeRl)
    AutoRelativeLayout personalinfoFreeRl;
    @BindView(R.id.personalinfo_logOut)
    TextView personalinfoLogOut;
    @BindView(R.id.activity_personnal_info)
    AutoRelativeLayout activityPersonnalInfo;
    @BindView(R.id.center_head)
    ImageView personalinfoIvHead;
    @BindView(R.id.progress_bar_PersonalInfo)
    AutoRelativeLayout progressBarPersonalInfo;

    private Context mContext = this;
    private String key;
    private String picToken;
    private WantuService wantuService;
    private UploadListener mListener = this;
    private String userName;
    private String phoneNum;
    private String[] strings;
    private int number = 0;
    private boolean isRunning = true;
    private String nick = "";
    private String gender = "";
    private String dateBitrh = "";
    private PushAgent mPushAgent;
    private boolean isChangeInfo = false;   //如果是跳转其他页面修改个人信息，返回后会执行onResume(),但是修改的数据显示内容不从服务器获取，而显示其他页面返回的数据
    private ArrayList<String> pics = new ArrayList<>();
    private static final int PICK_PHOTO = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnal_info);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0, true);
        AppManager.getAppManager().addActivity(this);
        userName = SharedPreferencesUtils.getInstance(mContext).getStringValue("userName");
        phoneNum = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.USER_PHONE_NUM);
        WantuService.enableHttpDNS();
        WantuService.enableLog();
        wantuService = WantuService.getInstance();
        wantuService.asyncInit(this);
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.onAppStart();
        EasyTransition.enter(this, 500, new AccelerateInterpolator(), null);
    }

    private void getReChargeAd() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getRechargeAd(MyApplication.getInstance().getRequestFieldMap())
                .enqueue(new Callback<RechargeAdBean>() {
                    @Override
                    public void onResponse(Call<RechargeAdBean> call, Response<RechargeAdBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                if (response.body().getAttachment().getAdList() == null || response.body().getAttachment().getAdList().size() == 0) {
                                    personalinfoArow5.setVisibility(View.INVISIBLE);
                                    personalinfoFreeRl.setClickable(false);
                                } else {
                                    personalinfoArow5.setVisibility(View.VISIBLE);
                                    personalinfoFreeRl.setClickable(true);
                                }
                            } else {
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<RechargeAdBean> call, Throwable t) {
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void getPersonalInfo() {
        progressBarPersonalInfo.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getPersonalInfo(MyApplication.getInstance().getRequestFieldMap()).enqueue(new Callback<PersonalInfoBean>() {
            @Override
            public void onResponse(Call<PersonalInfoBean> call, Response<PersonalInfoBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        if (response.body().getAttachment().getAuthStatus() == 0) {
                            personalinfoSexRl.setClickable(true);
                            personalinfoBathRl.setClickable(true);
                            personalinfoArow2.setVisibility(View.VISIBLE);
                            personalinfoArow3.setVisibility(View.VISIBLE);
                            if (response.body().getAttachment().getGender() == 1) {
                                personalinfoTvSex.setText("男");
                            } else if (response.body().getAttachment().getGender() == 2) {
                                personalinfoTvSex.setText("女");
                            }
                            personalinfoTvBath.setText(response.body().getAttachment().getDateOfBirth());
                        } else if (response.body().getAttachment().getAuthStatus() == 1) {
                            personalinfoSexRl.setClickable(false);
                            personalinfoBathRl.setClickable(false);
                            personalinfoArow2.setVisibility(View.INVISIBLE);
                            personalinfoArow3.setVisibility(View.INVISIBLE);
                            if (response.body().getAttachment().getGender() == 1) {
                                personalinfoTvSex.setText("男");
                            } else if (response.body().getAttachment().getGender() == 2) {
                                personalinfoTvSex.setText("女");
                            }
                            personalinfoTvBath.setText(response.body().getAttachment().getDateOfBirth());
                        }

                        personalinfoTvPhone.setText(response.body().getAttachment().getPhoneNum());
                        if (!isChangeInfo) {  //如果是修改个人信息后执行onResume(),不从服务器获取
                            personalinfoTvNi.setText(response.body().getAttachment().getNick());
                            isChangeInfo = false;
                        }

                        if (!isChangeInfo) {  //如果是修改个人信息后执行onResume(),不从服务器获取
                            ImageLoader.getInstance().displayCricleImage(getApplicationContext(), MyApplication.getInstance().getImageUrl() + response.body().getAttachment().getPicture(), personalinfoIvHead);
                            isChangeInfo = false;
                        }

                        if (response.body().getAttachment().getIsBikeBailFree() == 0) {
                            personalinfoTvFree.setText("未通过");
                            //personalinfoTvFree.setTextColor(Color.RED);
                            getReChargeAd();

                        } else if (response.body().getAttachment().getIsBikeBailFree() == 1) {
                            personalinfoTvFree.setText("通过");
                            //personalinfoTvFree.setTextColor(Color.rgb(70,200,120));

                            personalinfoArow5.setVisibility(View.INVISIBLE);

                            personalinfoFreeRl.setClickable(false);
                        }
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    progressBarPersonalInfo.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<PersonalInfoBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
                progressBarPersonalInfo.setVisibility(View.GONE);
            }
        });
    }

    @OnClick({R.id.personalinfo_logOut, R.id.personalinfo_return, R.id.center_head, R.id.personalinfo_headerRl, R.id.personalinfo_niRl, R.id.personalinfo_sexRl, R.id.personalinfo_bathRl, R.id.personalinfo_phoneRl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.personalinfo_return:
                EasyTransition.exit(this, 300, new AccelerateInterpolator());
                break;
            case R.id.center_head:
            case R.id.personalinfo_headerRl: {
                isChangeInfo = true;
                //跳转图片选择
                Intent intent = new Intent(PersonalInfoActivity.this, PhotoPickerActivity.class);
                intent.putExtra(PhotoPickerActivity.EXTRA_SHOW_CAMERA, true);
                intent.putExtra(PhotoPickerActivity.EXTRA_SELECT_MODE, PhotoPickerActivity.MODE_SINGLE);
                intent.putExtra(PhotoPickerActivity.EXTRA_CROP_IMAGE, true);
                //intent.putExtra(PhotoPickerActivity.EXTRA_MAX_MUN, PhotoPickerActivity.DEFAULT_NUM);
                startActivityForResult(intent, PICK_PHOTO);

            }
            break;
            case R.id.personalinfo_niRl: {
                isChangeInfo = true;
                Intent intent = new Intent();
                intent.putExtra("name", personalinfoTvNi.getText().toString());
                intent.setClass(PersonalInfoActivity.this, ChangeNameActivity.class);
                startActivityForResult(intent, Constants.REQUEST_CHANGE_INFO);
            }
            break;
            case R.id.personalinfo_sexRl:
                final KuaituDialog kuaituDialog = new KuaituDialog(this);
                kuaituDialog.setTitle(getString(R.string.dialogTitle));
                kuaituDialog.setMessage("请选择性别");
                kuaituDialog.setYesOnclickListener("男", new KuaituDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        personalinfoTvSex.setText("男");
                        gender = "1";
                        commitInfo();
                        kuaituDialog.dismiss();
                    }
                });
                kuaituDialog.setNoOnclickListener("女", new KuaituDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        personalinfoTvSex.setText("女");
                        gender = "2";
                        commitInfo();
                        kuaituDialog.dismiss();
                    }
                });
                kuaituDialog.setCancelable(false);
                kuaituDialog.show();
                break;
            case R.id.personalinfo_bathRl:
                View popupView = getLayoutInflater().inflate(R.layout.birthpick_popupwindow, null);
                final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                popupWindow.showAsDropDown(findViewById(R.id.personalinfo_toolbar), 0, 0);
                final DatePicker datePicker = (DatePicker) popupView.findViewById(R.id.datePicker);

                popupView.findViewById(R.id.datePicker_confim).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String year = datePicker.getYear() + "";
                        String month = (datePicker.getMonth()) + 1 + "";
                        String day = datePicker.getDayOfMonth() + "";
                        dateBitrh = year + "-" + month + "-" + day;
                        personalinfoTvBath.setText(dateBitrh);
                        commitInfo();
                        popupWindow.dismiss();
                    }
                });
                break;
            case R.id.personalinfo_phoneRl:
                break;
            case R.id.personalinfo_freeRl:
                startActivity(new Intent(PersonalInfoActivity.this, ChargeMoneyActivity.class));
                break;
            case R.id.personalinfo_logOut:
                String uid = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.USER_ID);
                logout(uid);
                break;
        }
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
                case 2:
                    //成功压缩
                    String pathName = msg.getData().getString("compressPath");
                    //上传图片
                    compressFile = new File(pathName);
                    uploadImage(compressFile);
                    break;
                case 3:
                    //压缩失败
                    progressBarPersonalInfo.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, "图片压缩失败，请重试", Toast.LENGTH_LONG).show();
                    break;
                case 4: //上传成功
                    commitAvatar();
                    break;
                case 5:
                    //上传失败
                    progressBarPersonalInfo.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, msg.getData().getString("failReason"), Toast.LENGTH_SHORT).show();
                    break;
                case 6:
                    //上传取消
                    progressBarPersonalInfo.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, "图片上传取消", Toast.LENGTH_SHORT).show();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private void logout(final String uid) {
        progressBarPersonalInfo.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).logoff(MyApplication.getInstance().getRequestFieldMap()).enqueue(new Callback<LogoffBean>() {
            @Override
            public void onResponse(Call<LogoffBean> call, Response<LogoffBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        ToastCompat.makeText(mContext, getResources().getString(R.string.logoffSuccess), Toast.LENGTH_SHORT).show();
                        try {
                            mPushAgent.removeAlias(uid, "USER", new UTrack.ICallBack() {
                                @Override
                                public void onMessage(boolean b, String s) {

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        //保留一些不能清除的数据
                        int depositBike = SharedPreferencesUtils.getInstance(mContext).getIntValue(SharedPreferencesKey.DEPOSIT_BIKE);
                        int depositCar = SharedPreferencesUtils.getInstance(mContext).getIntValue(SharedPreferencesKey.DEPOSIT_CAR);
                        int isAliPayOpen = SharedPreferencesUtils.getInstance(mContext).getIntValue(SharedPreferencesKey.IS_ALIPAY_OPEN);
                        int isWxPayOpen = SharedPreferencesUtils.getInstance(mContext).getIntValue(SharedPreferencesKey.IS_WXPAY_OPEN);
                        int isShareInvite = SharedPreferencesUtils.getInstance(mContext).getIntValue(SharedPreferencesKey.IS_SHARE_INVITE);
                        int isCarPrepare = SharedPreferencesUtils.getInstance(mContext).getIntValue(SharedPreferencesKey.IS_CAR_PREPARE);

                        SharedPreferencesUtils.getInstance(mContext).removeAll();

                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.DEPOSIT_BIKE, depositBike);
                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.DEPOSIT_CAR, depositCar);
                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.IS_ALIPAY_OPEN, isAliPayOpen);
                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.IS_WXPAY_OPEN, isWxPayOpen);
                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.IS_SHARE_INVITE, isShareInvite);
                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.IS_CAR_PREPARE, isCarPrepare);


                        MyApplication.getInstance().setUid(null);
                        MyApplication.getInstance().setToken(null);
                        VerControlUtils verControlUtils = new VerControlUtils(mContext);
                        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.OLD_VERSION_CODE, verControlUtils.getVersionCode());
                        Intent intent = new Intent(mContext, MainActivity.class);
                        intent.putExtra("isContinue", false);
                        startActivity(intent);
                        finish();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    progressBarPersonalInfo.setVisibility(View.GONE);
                } else {
                    progressBarPersonalInfo.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<LogoffBean> call, Throwable t) {
                progressBarPersonalInfo.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
                Log.i("neterror", "011" + t);
            }
        });

    }

    private File originalFile;
    private File compressFile;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_CHANGE_INFO && resultCode == Constants.REQUEST_CHANGE_INFO) {
            nick = data.getStringExtra("name");
            personalinfoTvNi.setText(nick);
            commitInfo();
        } else if (requestCode == PICK_PHOTO) {   //获得图片后
            if (resultCode == Activity.RESULT_OK) {
                ArrayList<String> result = data.getStringArrayListExtra(PhotoPickerActivity.KEY_RESULT);
                if (result == null || result.size() == 0) {
                    return;
                }
                progressBarPersonalInfo.setVisibility(View.VISIBLE);
                compressImage(result.get(0));

            }
        }
    }

    // 压缩图片
    private void compressImage(String path) {
        Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFd(path, 100, 100);
        String fileName = SDCardUtils.createFileName("Kuaitu", ".jpg");
        SDCardUtils.saveBitmapToSDCardPrivateCacheDir(bitmap, fileName, mContext);
        Message message = new Message();
        message.arg1 = 2;
        Bundle b = new Bundle();
        b.putString("compressPath", getExternalCacheDir().getAbsolutePath() + File.separator + fileName);
        message.setData(b);
        handler.sendMessage(message);
    }


    //提交生日 性别  昵称
    private void commitInfo() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getChangePersonalInfo(MyApplication.getInstance().getRequestFieldMap(), nick, gender, dateBitrh).enqueue(new Callback<ChangeInfoBean>() {
            @Override
            public void onResponse(Call<ChangeInfoBean> call, Response<ChangeInfoBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        ToastCompat.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<ChangeInfoBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }


    //获得要上传图片的参数，并上传图片
    private void uploadImage(final File picFile) {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).uploadImage(MyApplication.getInstance().getRequestFieldMap(), "jpg", "", "", "", "1", "0", "kuaitu").enqueue(new Callback<UploadImageBean>() {
            @Override
            public void onResponse(Call<UploadImageBean> call, Response<UploadImageBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        picToken = response.body().getAttachment().getToken();
                        key = response.body().getAttachment().getKey();
                        //百川上传图片
                        wantuService.upload(picFile, mListener, picToken);
                    } else {
                        progressBarPersonalInfo.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    progressBarPersonalInfo.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UploadImageBean> call, Throwable t) {
                progressBarPersonalInfo.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    //百川上传
    @Override
    public void onUploading(UploadTask uploadTask) {

    }

    @Override
    public void onUploadFailed(UploadTask uploadTask, FailReason failReason) {
        Message message = new Message();
        message.arg1 = 5;
        Bundle b = new Bundle();
        b.putString("failReason", failReason.getMessage());
        message.setData(b);
        handler.sendMessage(message);
    }

    @Override
    public void onUploadCancelled(UploadTask uploadTask) {
        Message message = new Message();
        message.arg1 = 6;
        Bundle b = new Bundle();
        message.setData(b);
        handler.sendMessage(message);
    }


    @Override
    public void onUploadComplete(UploadTask uploadTask) {
        Message message = new Message();
        message.arg1 = 4;
        Bundle b = new Bundle();
        message.setData(b);
        handler.sendMessage(message);
    }

    private void commitAvatar() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).commitAvatar(MyApplication.getInstance().getRequestFieldMap(), " ", key).enqueue(new Callback<CommitCertificateBean>() {
            @Override
            public void onResponse(Call<CommitCertificateBean> call, Response<CommitCertificateBean> response) {
                progressBarPersonalInfo.setVisibility(View.GONE);
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        //修改成功
                        SharedPreferencesUtils.getInstance(mContext).putStringValue("avatarPath", compressFile.getAbsolutePath());
                        //从私有目录读取图片至头像
                        ImageLoader.getInstance().displayCricleImage(getApplicationContext(), compressFile, personalinfoIvHead);

                        ToastCompat.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CommitCertificateBean> call, Throwable t) {
                progressBarPersonalInfo.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
                Log.i("neterror", "007");
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        getPersonalInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        EasyTransition.exit(this, 300, new AccelerateInterpolator());
    }
}
